package com.atguigu.gmall.payment.service.impl;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.*;
import com.alipay.api.response.*;
import com.atguigu.gmall.model.enums.PaymentStatus;
import com.atguigu.gmall.model.enums.PaymentType;
import com.atguigu.gmall.model.order.OrderInfo;
import com.atguigu.gmall.model.payment.PaymentInfo;
import com.atguigu.gmall.order.client.OrderFeignClient;
import com.atguigu.gmall.payment.config.AlipayConfig;
import com.atguigu.gmall.payment.service.AlipayService;
import com.atguigu.gmall.payment.service.PaymentService;
import lombok.SneakyThrows;
import org.redisson.misc.Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * @author Sanmier
 * @create 2020-12-21 19:01
 */
@Service
public class AlipayServiceImpl implements AlipayService {

    @Autowired
    private PaymentService paymentService;

    @Autowired
    private OrderFeignClient orderFeignClient;

    @Autowired
    private AlipayClient alipayClient;

    // 支付的接口
    @Override
    public String createaliPay(Long orderId) throws AlipayApiException {

        // 调用一个保存交易记录的方法
        // 根据orderId查询orderInfo
        OrderInfo orderInfo = orderFeignClient.getOrderInfo(orderId);

        if ("CLOSED".equals(orderInfo.getOrderStatus())||"PAID".equals(orderInfo.getOrderStatus())){
            return "订单已经支付或者已经关闭!";
        }
        paymentService.savePaymentInfo(orderInfo, PaymentType.ALIPAY);

        // 生成二维码
        // 修改数据  alipayClient操作支付宝所有接口的关键类   可以将alipayClient  注入到spring容器中
        // AlipayClient alipayClient =  new DefaultAlipayClient("https://openapi.alipay.com/gateway.do" , APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);  //获得初始化的AlipayClient
        AlipayTradePagePayRequest alipayRequest =  new  AlipayTradePagePayRequest(); //创建API对应的request
        alipayRequest.setReturnUrl(AlipayConfig.return_payment_url );
        alipayRequest.setNotifyUrl(AlipayConfig.notify_payment_url ); //在公共参数中设置回跳和通知地址

        // 封装的业务参数
        HashMap<String, Object> map = new HashMap<>();
        map.put("out_trade_no",orderInfo.getOutTradeNo());
        map.put("product_code","FAST_INSTANT_TRADE_PAY");
//        map.put("total_amount",orderInfo.getTotalAmount());
        map.put("timeout_express", "10m");
        map.put("total_amount","0.01");
        map.put("subject", orderInfo.getTradeBody());

        alipayRequest.setBizContent(JSON.toJSONString(map));

//        alipayRequest.setBizContent( "{"  +
//                                             "    \"out_trade_no\":\"20150320010101001\","  +
//                                             "    \"product_code\":\"FAST_INSTANT_TRADE_PAY\","  +
//                                             "    \"total_amount\":88.88,"  +
//                                             "    \"subject\":\"Iphone6 16G\","  +
//                                             "    \"body\":\"Iphone6 16G\","  +
//                                             "    \"passback_params\":\"merchantBizType%3d3C%26merchantBizNo%3d2016010101111\","  +
//                                             "    \"extend_params\":{"  +
//                                             "    \"sys_service_provider_id\":\"2088511833207846\""  +
//                                             "    }" +
//                                             "  }" ); //填充业务参数

        //返回表单的数据字符串  在控制器上可以直接写 @ResponseBody注解直接将字符串显示到页面
        return alipayClient.pageExecute(alipayRequest).getBody();  //调用SDK生成表单

    }


    //退款
    @Override
    public boolean refund(Long orderId) {
        //根据orderId来获取orderInfo
        OrderInfo orderInfo = orderFeignClient.getOrderInfo(orderId);
        //退款实现
        AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
        HashMap<String, Object> map = new HashMap<>();
        map.put("out_trade_no", orderInfo.getOutTradeNo());
        map.put("refund_amount", "0.01");
        map.put("refund_reason", "太贵了");
        request.setBizContent(JSON.toJSONString(map));
        AlipayTradeRefundResponse response = null;
        try {
            response = alipayClient.execute(request);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        if(response.isSuccess()){
            System.out.println("退款调用成功");
            //修改本地订单状态  修改交易记录  关闭支付宝的交易
            PaymentInfo paymentInfo = new PaymentInfo();
            paymentInfo.setPaymentStatus(PaymentStatus.ClOSED.name());
            paymentService.updatePaymentInfo(orderInfo.getOutTradeNo(), PaymentType.ALIPAY.name(), paymentInfo);
            return true;
        } else {
            System.out.println("退款调用失败");
            return false;
        }
    }


    //关闭交易
    @SneakyThrows
    @Override
    public Boolean closePay(Long orderId) {
        OrderInfo orderInfo = orderFeignClient.getOrderInfo(orderId);
        AlipayTradeCloseRequest request = new AlipayTradeCloseRequest();
        HashMap<String, Object> map = new HashMap<>();
        // map.put("trade_no",paymentInfo.getTradeNo()); // 从paymentInfo 中获取！
        map.put("out_trade_no",orderInfo.getOutTradeNo());
        map.put("operator_id","YX01");

        request.setBizContent(JSON.toJSONString(map));

        AlipayTradeCloseResponse response = alipayClient.execute(request);
        if (response.isSuccess()){
            System.out.println("关闭交易调用成功");
            return true;
        }else {
            System.out.println("关闭交易调用失败");
            return false;
        }
    }


    //根据订单查询是否支付成功！
    @SneakyThrows
    @Override
    public Boolean checkPayment(Long orderId) {
        //根据订单Id  查询订单信息
        OrderInfo orderInfo = orderFeignClient.getOrderInfo(orderId);
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();

        HashMap<String, Object> map = new HashMap<>();
        map.put("out_trade_no", orderInfo.getOutTradeNo());
        // 根据out_trade_no 查询交易记录
        request.setBizContent(JSON.toJSONString(map));

        AlipayTradeQueryResponse response = alipayClient.execute(request);
        if (response.isSuccess()){
//            if (response.getTradeStatus().equals("TRADE_SUCCESS")||"TRADE_FINISHED".equals(response.getTradeStatus())){
//                System.out.println("有交易记录，同时用户一定支付成功了");
//                return true;
//            }
            System.out.println("支付有交易记录");
            return true;
        }else {
            System.out.println("支付调用失败");
            return false;
        }
    }
}
